import { API_BASEURL } from '@/constants/api';
import dayjs from 'dayjs';

export const formatPhone = (phone: string) => {
  return '+86' + phone;
};

export const formatCountDown = (countdown: number) => {
  return Math.round(countdown / 1000);
};

export const formatUrl = (path: string) => {
  const result = path.split('\\\\').join('/');
  return API_BASEURL + '/' + result;
};

export const formatTime = (data: string, format = 'YYYY-MM-DD HH:mm') => {
  return dayjs(data).format(format);
};

export const formatCarts = (carts: API.CartStruct[]) => {
  const shopList: CartShow[] = [];
  carts.forEach((value) => {
    const user = value.goods?.user;
    if (user && !shopList.find((value) => value.id === user.id)) {
      const shop: CartShow = { ...user, carts: [value] };
      shopList.push(shop);
    } else {
      shopList.find((value) => value.id === user?.id)?.carts.push(value);
    }
  });
  return shopList;
};

export const createHtml = (value: string) => ({ __html: value.replace(/(\r\n|\n|\r)/gm, "<br/>") });
